package gov.dhs.cis.soa.data;

import gov.dhs.cis.soa.Utils;
import gov.dhs.cis.soa.Constants.DataDirType;
import gov.dhs.cis.soa.Constants.EntityType;

public class RelatedDataReference extends AbstractSOAData {
	public static final String IN_TYPE = "I";
	public static final String OUT_TYPE = "O";
	
	public int id;
	public int refid;
	public String reftype;
	public String direction;
	public DataClass data;
	public transient String refEntityHashableId;
	
	@Override
	public String toString() {
		return "DataClassReference [refid = " + refid + ", reftype=" + reftype + "]";
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		RelatedDataReference other = (RelatedDataReference) obj;
		if (refid == other.refid && data.id == other.data.id && reftype.equalsIgnoreCase(other.reftype))
			return true;
		return false;
	}
	
	public static RelatedDataReference getNewDataReference(DataClass dataO, AbstractSOAData refEntity, DataDirType dirType) {
		RelatedDataReference refItem = null;
		
		if(dataO == null)
			return null;
		refItem = new RelatedDataReference();
		refItem.id = Utils.getNextId();
		refItem.refid = (int)refEntity.getId();
		refItem.reftype = refEntity.getUMLType().toString();
		refItem.direction = dirType.toString();
		refItem.data = dataO;
		refItem.refEntityHashableId = refEntity.getHashableId();
		return refItem;
	}
	
//	public static String getUMLId(int entityId) {
//		return EntityType.DREF + "_" + entityId;
//	}
	@Override
	public long getId() {
		return id;
	}
	@Override
	public String getHashableId() {
		return reftype + " " + refEntityHashableId + " " + direction + " " + data.getHashableId();
	}
	
	@Override
	public EntityType getUMLType() {
		return EntityType.DREF;
	}
}
